package com.linkoog.devtools.code_generator.wizard.model;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.treeStructure.treetable.TreeTableModelAdapter;
import com.intellij.ui.treeStructure.treetable.TreeTableTree;
import com.linkoog.devtools.code_generator.CodeGenIcons;
import com.linkoog.devtools.code_generator.common.JavaType;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.util.EventObject;

public class OprateRowCellEditer extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {

    private ModelJsonTreeTable.ItemInfo itemInfo;
    private JPanel renderPanel;
    private JPanel editerPanel;

    public OprateRowCellEditer(ModelJsonTreeTable.ItemInfo itemInfo){
        this.itemInfo = itemInfo;
        renderPanel = new JBPanel(new BorderLayout());
        editerPanel = new JBPanel(new BorderLayout());
    }

    public Component getEditerComponent() {
        return editerPanel;
    }

    @Override
    public boolean isCellEditable(EventObject e) {
        return true;
    }

    private void initPanel(JPanel jPanel, ModelJsonTreeTable table){
        TreeTableTree tableTree = table.getTree();
        ModelJsonTreeTableModel model = (ModelJsonTreeTableModel)tableTree.getModel();
        TreeTableModelAdapter modelAdapter = (TreeTableModelAdapter)table.getModel();

        DefaultActionGroup actionGroup = new DefaultActionGroup();
        AnAction addAction = new DumbAwareAction("Add", "", CodeGenIcons.ADD){
            @Override
            public void actionPerformed(@NotNull AnActionEvent e) {
                ModelJsonTreeTable.ItemInfo item = new ModelJsonTreeTable.ItemInfo();
                item.setName("field" + (itemInfo.getChildCount()+1));
                item.setNameEditable(true);
                item.setChecked(true);
                item.setCheckEditable(true);
                item.setType(JavaType.STRING);
                item.setRemark("");

                model.insertNodeInto(item, itemInfo, itemInfo.getChildCount());
                table.expandAll();
            }
        };
        AnAction deleteAction = new DumbAwareAction("Delete", "", CodeGenIcons.DELETE){
            @Override
            public void actionPerformed(@NotNull AnActionEvent e) {
                tableTree.expandPath(new TreePath(itemInfo));
                TreeNode parent = itemInfo.getParent();
                if (parent != null){
                    model.removeNodeFromParent(itemInfo);
                }
                OprateRowCellEditer.this.stopCellEditing();
            }
        };
        if (JavaType.OBJECT == itemInfo.getType()){
            actionGroup.add(addAction);
        }

        if (! itemInfo.isRootFlag()){
            actionGroup.add(deleteAction);
        }


        ActionManager actionManager = ActionManager.getInstance();
        ActionToolbar actionToolbar = actionManager.createActionToolbar(ActionPlaces.TOOLBAR, actionGroup, true);
        actionToolbar.setMinimumButtonSize(new Dimension(5, -1));
        actionToolbar.setTargetComponent(jPanel);

        JComponent context = actionToolbar.getComponent();
        context.setBackground(table.getBackground());
        jPanel.setBackground(table.getBackground());
        jPanel.add(context, BorderLayout.NORTH);
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        initPanel(editerPanel, (ModelJsonTreeTable)table);
        return editerPanel;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        //renderPanel = new JPanel();
        System.out.println(renderPanel.isPaintingForPrint());
        initPanel(renderPanel, (ModelJsonTreeTable)table);
        return renderPanel;
    }

    @Override
    public Object getCellEditorValue() {
        return itemInfo;
    }

}
